//
//  config.swift
//  jocool
//
//  Created by tong on 16/6/11.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import Foundation
import UIKit
import Eelay




public let Swidth:CGFloat = UIScreen.main.bounds.size.width
public var Sheight:CGFloat{
    get{
        return UIScreen.main.bounds.size.height
    }
}

public var __width:CGFloat = 375
public let InitSheight:CGFloat = UIScreen.main.bounds.size.height
public let IPhone_4:Bool = (UIScreen.main.bounds.size.height <= 480)
public let IPhone_5:Bool = (UIScreen.main.bounds.size.height <= 568)


extension TP {
    
    public enum Notice:String,NotifyType {
        case traitCollectionDidChange = "traitCollectionDidChange"
        case localizableDidChange = "localizableDidChange"

        
        public var name: Notification.Name{
            return "TP.Notice.\(Notice.self).\(self.rawValue)".notice_name
        }
    }

    
    static public let width:CGFloat = Swidth
    static public let height:CGFloat = Sheight
    
    static public var tatusBarHeight:CGFloat{
        get{
            return UIApplication.shared.statusBarFrame.height
        }
    }
    public static var isNotchScreen: Bool {

        if UIDevice.current.userInterfaceIdiom == .pad {
            return false
        }

        let size = UIScreen.main.bounds.size
        let notchValue: Int = Int(size.width/size.height * 100)

        if 216 == notchValue || 46 == notchValue {

            return true
        }

        return false
    }
    public static var hasAreaInsets: Bool {
        if #available(iOS 11, *) {
              guard let w = UIApplication.shared.delegate?.window, let unwrapedWindow = w else {
                  return false
              }
              
              if unwrapedWindow.safeAreaInsets.left > 0 || unwrapedWindow.safeAreaInsets.bottom > 0 {
//                  print(unwrapedWindow.safeAreaInsets)
                  return true
              }
        }
        return false
    }
    
//    static public var hasAreaInsets:Bool{
//        get{
////            return false
//            var ok = false
//            if let window = UIApplication.shared.delegate?.window{
//                if let window_ = window{
//                    if #available(iOS 11.0, *) {
//                        if window_.safeAreaInsets.bottom > 0{
//                            ok = true
//                        }
//                    } else {
//                        // Fallback on earlier versions
//                    }
//                }
//
//            }
//            return ok
//        }
//    }
    
    static public var navigationBarHeight:CGFloat = 44
    static public var topHeight:CGFloat{
        get{
            return TP.tatusBarHeight + TP.navigationBarHeight
        }
    }
    static public var bottomHeight:CGFloat = 49
    static public var tabbarHeight:CGFloat = UITabBar().frame.size.height
    
    static public let separatorHeight = 1.0/UIScreen.main.scale
    
    static var _bottomLayoutGuide:CGFloat = 0
    static public var bottomLayoutGuide:CGFloat{
        get{
            //            return __controller.bottomLayoutGuide.length
            if _bottomLayoutGuide > 0{
                return _bottomLayoutGuide
            }
            else{
                _bottomLayoutGuide = __controller.bottomLayoutGuide.length
                return _bottomLayoutGuide
            }
        }
        set{
            _bottomLayoutGuide = newValue
        }
    }
    
    static var _topLayoutGuide:CGFloat = 0
    static public var topLayoutGuide:CGFloat{
        get{
            return TP._topLayoutGuide
        }set{
            TP._topLayoutGuide = newValue
        }
    }
}



public class JocoolAssets: NSObject {
    
    public class func bundledImage(named name: String) -> UIImage {
        //        let bundle = Bundle(for: JocoolAssets.self)
        //        let image = UIImage(named: name, in:bundle, compatibleWith:nil)
        let nundle = Bundle(for: JocoolAssets.self)
        let image = UIImage(named: name, in: nundle, compatibleWith: nil)
        if let image = image {
            return image
        }
        return UIImage()
    }
}




extension Bundle{
    
    public class func path(for aClass: Swift.AnyClass,bundleName:String,fileName:String,type:String) -> String? {
        
        let soweb = Bundle(for:aClass).path(forResource: bundleName, ofType: "bundle")
        let bd = Bundle(path: soweb!)?.path(forResource: "\(fileName)", ofType: "\(type)")
        return bd
    }
    
    public class func path(bundleName:String,fileName:String,type:String) -> String? {
        let soweb = Bundle.main.path(forResource: bundleName, ofType: "bundle")
        let bd = Bundle(path: soweb!)?.path(forResource: "\(fileName)", ofType: "\(type)")
        return bd
    }
    
}
extension CGFloat {
    public static var hpadding:CGFloat = 10
    public static var vpadding:CGFloat = 10
    public static var vspace:CGFloat = 10
    public static var hspace:CGFloat = 10
    public static var radius:CGFloat = 6
   
    public static let separator = 1.0/UIScreen.main.scale

}


public struct AppTheme {
    
    public static var main_hex:String = "#5FB187"
    public static var table_bg_hex:String = "#EFEFEF"
    public static var separator_hex:String = "#EFEFEF"
    public static var placeholder_hex:String = "#bbbbbb"
    public static var border_hex:String = "#ccc9"
    public static var nav_back_img:UIImage? = nil
    
    public static var edge:CGFloat = 20.co
    public static var radius:CGFloat = 4
    
    public static var tableBgColor:UIColor = .color(light: "#efefef", dark: "#000")
    public static var separatorColor:UIColor = .color(light: "#efefef", dark: "#000")
    public static var foregroundColor:UIColor = .color(light: "#efefef", dark: "#000")
}


extension UIColor{
    public static var darkHex = "#1C1D1A"
    public static var lightHex = "#FFFFFF"
    
    static var __separator:UIColor = .color(light: "#afafaf77", dark: "#a1a1a133")
    static var __cellBackground:UIColor = .color(light: "#fff", dark: "#292929")
    static var __tableBackground:UIColor = .color(light: "#f1f1f1", dark: "#202020")
    static var __foregroundWeak:UIColor = .color(light: "#908E8E", dark: "#CCCCCC")
    static var __foregroundStrong:UIColor = .color(light: "#182434", dark: "#ffffff")
    static var __foreground:UIColor = .color(light: "#182434", dark: "#e9e9e9")
    static var __background:UIColor = .color(light: "#fff", dark: "#232323")
    static var __backgroundWeak:UIColor = .color(light: "#c1c1c1", dark: "#66666633")
    static var __backgroundStrong:UIColor = .color(light: "#fff", dark: "#111")
    static var __tint:UIColor = .color(light: "#1C1D1A", dark: "#1C1D1A")
    static var __important:UIColor = .color(light: "#1C1D1A", dark: "#1C1D1A")
    static var __placeholder:UIColor = .color(light: "#D1D2D9", dark: "#D1D2D9")
    static var __disable:UIColor = .color(light: "#aaaa", dark: "#aaa2")
    static var __weak:UIColor = .color(light: "#999", dark: "#555")
    static var __second:UIColor = .color(light: "#999", dark: "#555")
    static var __weakBorder:UIColor = .color(light: "#8883", dark: "#8883")


    static var __box:UIColor = .color(light: "#fff", dark: "#252525")






    
    @objc public class var _separator:UIColor{set{UIColor.__separator = newValue}get{return MoTheme.global._color(for:UIColor.__separator)}
    }
    @objc public class var _cellBackground:UIColor{
        set{UIColor.__cellBackground = newValue}
        get{return MoTheme.global._color(for:UIColor.__cellBackground)}}

    @objc public dynamic class var _tableBackground:UIColor{
        set{UIColor.__tableBackground = newValue}
        get{return MoTheme.global._color(for:UIColor.__tableBackground)}}

    @objc public dynamic class var _foregroundWeak:UIColor{
         set{UIColor.__foregroundWeak = newValue}
         get{return MoTheme.global._color(for:UIColor.__foregroundWeak)}}
    
    @objc public dynamic class var _foregroundStrong:UIColor{
         set{UIColor.__foregroundStrong = newValue}
         get{return MoTheme.global._color(for:UIColor.__foregroundStrong)}
    }
    @objc public dynamic class var _foreground:UIColor{
         set{UIColor.__foreground = newValue}
         get{return MoTheme.global._color(for:UIColor.__foreground)}
    }

    @objc public dynamic class var _background:UIColor{
       set{UIColor.__background = newValue}
       get{return MoTheme.global._color(for:UIColor.__background)}
    }

    @objc public dynamic class var _backgroundWeak:UIColor{
        set{UIColor.__backgroundWeak = newValue}
        get{return MoTheme.global._color(for:UIColor.__backgroundWeak)}
    }

    @objc public dynamic class var _backgroundStrong:UIColor{
          set{UIColor.__backgroundStrong = newValue}
          get{return MoTheme.global._color(for:UIColor.__backgroundStrong)}
    }
    
    @objc public dynamic class var _tint:UIColor{
          set{UIColor.__tint = newValue}
          get{return MoTheme.global._color(for:UIColor.__tint)}
    }
    
    @objc public dynamic class var _important:UIColor{
          set{UIColor.__important = newValue}
          get{return MoTheme.global._color(for:UIColor.__important)}
    }
    
    @objc public dynamic class var _placeholder:UIColor{
          set{UIColor.__placeholder = newValue}
          get{return MoTheme.global._color(for:UIColor.__placeholder)}
    }

    @objc public dynamic class var _disable:UIColor{
          set{UIColor.__disable = newValue}
          get{return MoTheme.global._color(for:UIColor.__disable)}
    }
    @objc public dynamic class var _weak:UIColor{
          set{UIColor.__weak = newValue}
          get{return MoTheme.global._color(for:UIColor.__weak)}
    }
    @objc public dynamic class var _second:UIColor{
          set{UIColor.__second = newValue}
          get{return MoTheme.global._color(for:UIColor.__second)}
    }
    
    @objc public dynamic class var _weakBorder:UIColor{
          set{UIColor.__weakBorder = newValue}
          get{return MoTheme.global._color(for:UIColor.__weakBorder)}
    }
    
    @objc public dynamic class var _box:UIColor{
          set{UIColor.__box = newValue}
          get{return MoTheme.global._color(for:UIColor.__box)}
    }


    
}

extension UIColor{
    public class func color(light:String,dark:String)->UIColor{
        switch MoTheme.global.mode {
        case .light:
            return UIColor(shex: light)
        case .dark:
            return UIColor(shex: dark)
        default:
            break
        }
        if #available(iOS 13.0, *) {
            return UIColor { (tcollection) -> UIColor in
                switch tcollection.userInterfaceStyle {
                case .dark:
                    return UIColor(shex: dark)
                default:
                    return UIColor(shex: light)
                }
            }
        } else {
            return UIColor(shex: light)
        }
    }
    
    public class func color(lColor:UIColor,dColor:UIColor)->UIColor{
        switch MoTheme.global.mode {
             case .light:
                 return lColor
             case .dark:
                 return dColor
             default:
                 break
             }
        if #available(iOS 13.0, *) {
            return UIColor { (tcollection) -> UIColor in
                switch tcollection.userInterfaceStyle {
                case .dark:
                    return dColor
                default:
                    return lColor
                }
            }
        } else {
            return lColor
        }
    }
    
    @objc public class dynamic var main_hex:String{
        get{
            return AppTheme.main_hex
        }
        set{
            AppTheme.main_hex = newValue
        }
    }
    @objc public static var main_color:UIColor{
        get{
            return UIColor(shex:main_hex)
        }
    }
    
    @objc public static dynamic var table_bg_hex:String{
        get{
            return AppTheme.table_bg_hex
        }
        set{
            AppTheme.table_bg_hex = newValue
        }
    }
    
    @objc public dynamic static var table_bg_color:UIColor{
        get{
            return AppTheme.tableBgColor
        }set{
            return AppTheme.tableBgColor = newValue
        }
    }
    
    @objc public dynamic  static var separator_hex:String{
        get{
            return AppTheme.separator_hex
        }
        set{
            AppTheme.separator_hex = newValue
        }
    }
    
    @objc public dynamic static var separator_color:UIColor{
        get{
            return ._separator
        }
    }
    
    @objc public dynamic static var placeholder_hex:String{
        get{
            return AppTheme.table_bg_hex
        }
        set{
            AppTheme.table_bg_hex = newValue
        }
    }
    
    @objc public dynamic static var placeholder_color:UIColor{
        get{
            return UIColor(shex:placeholder_hex)
        }
    }
    
    
}



